package com.yiwei.zxp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.yiwei.zxp.BussinessMainActivity;
import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.BoothPacketActivity;
import com.yiwei.zxp.activity.BussinessMoneyRecordActivity;
import com.yiwei.zxp.activity.BussinessOrderDetailActivity;
import com.yiwei.zxp.activity.BuyerOrderDetailActivity;
import com.yiwei.zxp.activity.LogisticsActivity;
import com.yiwei.zxp.activity.MyCommissionActivity;
import com.yiwei.zxp.activity.MyOrderDetailActivity;
import com.yiwei.zxp.activity.MyShareMoneyActivity;
import com.yiwei.zxp.adapter.BussinessMsgAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.MessageTypeBean;
import com.yiwei.zxp.event.HomePagerEvent;
import com.yiwei.zxp.event.MainPacketEvent;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.OnLoadeMoreListener;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-6-15.
 */

public class BussinessMsgFragment extends BaseFragment {
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    Unbinder unbinder;
    private int type;
    private BussinessMsgAdapter adapter;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getMsg(page, type);
        }
    };

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_bussiness_msg;
    }

    @Override
    protected void initView() {
        if (getArguments() != null) {
            type = getArguments().getInt("id", 0);
        }
        list.addOnScrollListener(onLoadeMoreListener);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage(1);
                getMsg(1, type);
            }
        });
        adapter = new BussinessMsgAdapter(getContext());
        adapter.setOnChooseListener(new BussinessMsgAdapter.OnChooseListener() {
            @Override
            public void onClick(String order_no, String child_type) {
//                物流", "订单
                if (type == 3) {
//                    物流
                    Intent intent = new Intent(getContext(), LogisticsActivity.class);
                    intent.putExtra("order_no", order_no);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                } else if (type == 2) {
//                    订单消息
//                    为1：表示卖家订单消息，为2：表示买家 订单消息，为3：表示展位订单
                    if (child_type.equals("1")) {
                        Intent intent = new Intent(getContext(), BussinessOrderDetailActivity.class);
                        intent.putExtra("order_no", order_no);
                        startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                    } else if (child_type.equals("2")) {
                        Intent intent = new Intent(getContext(), BuyerOrderDetailActivity.class);
                        intent.putExtra("order_no", order_no);
                        startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                    } else if (child_type.equals("3")) {
                        Intent intent = new Intent(getContext(), MyOrderDetailActivity.class);
                        intent.putExtra("order_number", order_no);
                        startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                    }
                } else if (type == 4) {
//                    账户消息
//                    1到我的淘淘币；2到我的佣金；3到我的佣金；4到进账中心；5到进账中心；6到进账中心
                    switch (child_type) {
                        case "1":
                            Util.startActivity(getActivity(), MyShareMoneyActivity.class);
                            break;
                        case "2":
                            Util.startActivity(getActivity(), MyCommissionActivity.class);
                            break;
                        case "3":
                            Util.startActivity(getActivity(), MyCommissionActivity.class);
                            break;
                        case "4":
                            Util.startActivity(getActivity(), BussinessMoneyRecordActivity.class);
                            break;
                        case "5":
                            Util.startActivity(getActivity(), BussinessMoneyRecordActivity.class);
                            break;
                        case "6":
                            Util.startActivity(getActivity(), BussinessMoneyRecordActivity.class);
                            break;
                    }
                } else if (type == 1) {
//                  系统消息
                    if (child_type.equals("1")) {
                        Util.startActivity(getActivity(), BoothPacketActivity.class);
                    } else if (child_type.equals("2")) {
                        EventBus.getDefault().post(new MainPacketEvent());
                        EventBus.getDefault().post(new HomePagerEvent(1));
                        Util.startActivity(getActivity(), BussinessMainActivity.class);
                        getActivity().finish();
                    }
                }
            }
        });
        list.setAdapter(adapter);
        onLoadeMoreListener.setPage(1);
        getMsg(1, type);
    }

    @Override
    protected void lazyLoad() {
        super.lazyLoad();
//        onLoadeMoreListener.setPage(1);
//        getMsg(1, type);
    }

    public void getMsg(int page, int type) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }

        if (refresh != null) {
            refresh.setRefreshing(true);
        }
        MessageTypeBean bean = new MessageTypeBean();
        bean.page = page + "";
        bean.type = type + "";
        Api.api_service.getMessageType(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
